package com.eastday.biz.impl;

import java.io.IOException;
import java.util.Set;
import java.util.Iterator;
import java.util.List;
import org.jdom.JDOMException;

import com.eastday.biz.GenerateHtmlBiz;
import com.eastday.dao.ExportNewsDao;
import com.eastday.util.BeanFactory;
import com.eastday.util.ParseConfigFactory;
import com.eastday.model.NewsInfoModel;
import com.eastday.model.NewsPrintModel;

import freemarker.template.TemplateException;

/**
 * 
 * @author dingxs
 *
 */
public class GenerateHtmlBizImpl extends CommonService implements GenerateHtmlBiz {
	
	public GenerateHtmlBizImpl()
	{
		super();
		try {
			ParseConfigFactory.constructUrlConfig();
		} catch (JDOMException e) {
			// TODO Auto-generated catch block
			this.exceptionHandler.writeToLog(e.getMessage());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			this.exceptionHandler.writeToLog(e.getMessage());
		}
	}

	@Override
	public void printData() {
		// TODO Auto-generated method stub
		Set urlSet = ParseConfigFactory.urlConfigModel.getUrlConfigMap().keySet();
		Iterator iterator = urlSet.iterator();
		while(iterator.hasNext())
		{
			ExportNewsDao exportNewsDao = (ExportNewsDao)BeanFactory.getBean("ExportNewsOracleDao");
			String url = iterator.next().toString();
			String filename = url.substring(url.lastIndexOf("/")+1);
			String filenameprefix = filename.substring(0,filename.indexOf("."));
			int maxPage = exportNewsDao.selectMaxPage(url);
			//重写首页
			List<NewsInfoModel> newsInfoModelListForIndex = exportNewsDao.ExportNews(0, url);
			NewsPrintModel newsPrintModel = new NewsPrintModel();
			newsPrintModel.setNewsInfoModeList(newsInfoModelListForIndex);
			newsPrintModel.setIsIndexPage(1);
			try {
				newsPrintModel.convertArrayToString(filenameprefix, maxPage);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (TemplateException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			//生成首页副本
			newsPrintModel = new NewsPrintModel();
			newsPrintModel.setNewsInfoModeList(newsInfoModelListForIndex);
			newsPrintModel.setIsIndexPage(2);
			try {
				newsPrintModel.convertArrayToString(filenameprefix, maxPage);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (TemplateException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			if(maxPage!=0)
			{
				//生成页
				exportNewsDao = (ExportNewsDao)BeanFactory.getBean("ExportNewsOracleDao");
				List<NewsInfoModel> newsInfoModelList = exportNewsDao.ExportNews(maxPage, url);
				newsPrintModel = new NewsPrintModel();
				newsPrintModel.setNewsInfoModeList(newsInfoModelList);
				newsPrintModel.setIsIndexPage(0);
				try {
					newsPrintModel.convertArrayToString(filenameprefix, maxPage);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (TemplateException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
		}
	}
	
	public void printData(String filename){
		
		ExportNewsDao exportNewsDao = (ExportNewsDao)BeanFactory.getBean("ExportNewsOracleDao");
		String filenameprefix = filename;
		int maxPage = exportNewsDao.selectMaxPage("");
		//重写首页
		List<NewsInfoModel> newsInfoModelListForIndex = exportNewsDao.ExportNews(0, "");
		NewsPrintModel newsPrintModel = new NewsPrintModel();
		newsPrintModel.setNewsInfoModeList(newsInfoModelListForIndex);
		newsPrintModel.setIsIndexPage(1);
		try {
			newsPrintModel.convertArrayToString(filenameprefix, maxPage);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			this.exceptionHandler.writeToLog(e.getMessage());
		} catch (TemplateException e) {
			// TODO Auto-generated catch block
			this.exceptionHandler.writeToLog(e.getMessage());
		}
		//生成首页副本
		newsPrintModel = new NewsPrintModel();
		newsPrintModel.setNewsInfoModeList(newsInfoModelListForIndex);
		newsPrintModel.setIsIndexPage(2);
		try {
			newsPrintModel.convertArrayToString(filenameprefix, maxPage);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			this.exceptionHandler.writeToLog(e.getMessage());
		} catch (TemplateException e) {
			// TODO Auto-generated catch block
			this.exceptionHandler.writeToLog(e.getMessage());
		}
		if(maxPage!=0)
		{
			//生成页
			exportNewsDao = (ExportNewsDao)BeanFactory.getBean("ExportNewsOracleDao");
			List<NewsInfoModel> newsInfoModelList = exportNewsDao.ExportNews(maxPage, "");
			newsPrintModel = new NewsPrintModel();
			newsPrintModel.setNewsInfoModeList(newsInfoModelList);
			newsPrintModel.setIsIndexPage(0);
			try {
				newsPrintModel.convertArrayToString(filenameprefix, maxPage);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				this.exceptionHandler.writeToLog(e.getMessage());
			} catch (TemplateException e) {
				// TODO Auto-generated catch block
				this.exceptionHandler.writeToLog(e.getMessage());
			}
		}
	}

}
